package com.china.api.web.dao.impl;

import java.util.List;

import javax.swing.text.AbstractDocument.Content;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;




import com.china.itapi.common.page.Page;

import static org.hibernate.criterion.Example.create;

/**
 * A data access object (DAO) providing persistence and search support for
 * Content entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.fdway.po.Content
 * @author MyEclipse Persistence Tools
 */

public class ContentDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ContentDAO.class);
	// property constants
	public static final String TYID = "tyid";
	public static final String NAME = "name";
	public static final String DETAIL = "detail";
	public static final String TIME = "time";
	public static final String BACK1 = "back1";
	public static final String BACK2 = "back2";
	public static final String BACK3 = "back3";
	public Session session=getSession();
	public Transaction tc;
	
	public void start(){
	 	tc=session.beginTransaction();
	 	tc.begin();
	 	session.clear();
	}
	
	public void commit(){
		tc.commit();
	}
	public void close(){
		session.close();
	}

	public void save(Content transientInstance) {
		log.debug("saving Content instance");
		try {
			session.save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	
	public void update(Content persistentInstance) {
		log.debug("updating Content instance");
		try {
			
			session.update(persistentInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	

	public void delete(Content persistentInstance) {
		log.debug("deleting Content instance");
		try {
			session.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Content findById(java.lang.Long id) {
		log.debug("getting Content instance with id: " + id);
		try {
			session.clear();
			Content instance = (Content) session.get(
					"com.fdway.po.Content", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Content> findByExample(Content instance) {
		log.debug("finding Content instance by example");
		try {
			session.clear();
			List<Content> results = (List<Content>) session
					.createCriteria("com.fdway.po.Content").add(
							create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Content instance with property: " + propertyName
				+ ", value: " + value);
		try {
			session.clear();
			String queryString = "from Content as model where model."
					+ propertyName + "= ? order by model.time desc";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, value);
			queryObject.setCacheable(false);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findTop6ByProperty(String propertyName, Object value) {
		log.debug("finding Content instance with property: " + propertyName
				+ ", value: " + value);
		try {
			session.clear();
			String queryString = "from Content as model where model."
					+ propertyName + "= ? order by model.time desc";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, value);
			queryObject.setFirstResult(0);
			queryObject.setMaxResults(6);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List findByPage(long tyid, Page page) {
		log.debug("finding Content instance with property: " + tyid
				+ ", value: " + page);
		try {
			session.clear();
			String queryString = "from Content as model where model.tyid= ? order by model.time desc";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, tyid);
			queryObject.setFirstResult(page.getBeginIndex());
			queryObject.setMaxResults(page.getEveryPage());
			queryObject.setCacheable(false);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Content> findByTyid(Object tyid) {
		return findByProperty(TYID, tyid);
	}
	
	public List<Content> findTop6ByTyid(Object tyid) {
		return findTop6ByProperty(TYID, tyid);
	}

	public List<Content> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<Content> findByDetail(Object detail) {
		return findByProperty(DETAIL, detail);
	}

	public List<Content> findByTime(Object time) {
		return findByProperty(TIME, time);
	}

	public List<Content> findByBack1(Object back1) {
		return findByProperty(BACK1, back1);
	}

	public List<Content> findByBack2(Object back2) {
		return findByProperty(BACK2, back2);
	}

	public List<Content> findByBack3(Object back3) {
		return findByProperty(BACK3, back3);
	}

	public List findAll() {
		log.debug("finding all Content instances");
		try {
			session.clear();
			String queryString = "from Content";
			Query queryObject = session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Content merge(Content detachedInstance) {
		log.debug("merging Content instance");
		try {
			session.clear();
			Content result = (Content) session.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Content instance) {
		log.debug("attaching dirty Content instance");
		try {
			session.clear();
			session.saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Content instance) {
		log.debug("attaching clean Content instance");
		try {
			session.clear();
			session.lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}